/**
 * @file winmain.c
 * @author Yao Zongyou <yaozy2010@gmail.com>
 * @date 2011-03-26
 */

#include "master.h"

#include <commctrl.h>

#include "comfunct/comfunct.h"
#include "wndproc.h"

/* Real WinMain Prototype */
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int);

/*
 * It seems MinGW doesn't like _tWinMain when _UNICODE is defined and 
 * generates an undefined reference to WinMain. So we provide a stub to 
 * keep it quiet.
 */
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hNull, 
			       LPSTR lpCmdLine, int nCmdShow)
{
	return _tWinMain(hInst, NULL, GetCommandLine(), nCmdShow);
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
			         LPTSTR lpCmdLine, int nCmdShow)
{
	const TCHAR szClassName[] = TEXT("AudioPlayer");
	WNDCLASSEX wc = {0};
	MSG Msg = {0};
	HACCEL hAccel = NULL;
	HWND hwnd = NULL;
	size_t cmdcharsize = 0;  /* length of command line passed to program */

	OleInitialize(NULL);
	InitCommonControls();

	SetupLanguage();

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadImage(hInstance, MAKEINTRESOURCE(IDR_ICON), IMAGE_ICON, 
				         32, 32, LR_SHARED);     
	/* The LR_SHARED flag leaves the OS to clean up the loaed image's memory */
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
	wc.lpszClassName = szClassName;
	wc.hIconSm = LoadImage(hInstance, MAKEINTRESOURCE(IDR_ICON), IMAGE_ICON, 
				           16, 16, LR_SHARED);
	
	if (!RegisterClassEx(&wc)) {
		ErrorInfo();
		ErrorMessage(GetLocalText(67));
		return 0;
	}
		
	hwnd = CreateWindowEx(WS_EX_CONTROLPARENT | WS_EX_ACCEPTFILES,
				szClassName, szClassName, 
				WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
				CW_USEDEFAULT, CW_USEDEFAULT, 340, 175, 
				NULL, NULL, hInstance, NULL);
	
	if (hwnd == NULL) {
//		ErrorInfo();
//		ErrorMessage(GetLocalText(68));
		return 0;
	}

//	hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_KEYACCEL));
	                                             /* Load the keyboard shortcuts */
	if (hAccel == NULL) {
//		ErrorInfo();
//		ErrorMessage(GetLocalText(69));
	}

//	cmdcharsize = _tcslen(lpCmdLine);
	
	if (cmdcharsize != 0) {
//		if (ParseCommandLine(lpCmdLine, cmdcharsize, hwnd) == false) {
//			ShowWindow(hwnd, nCmdShow);
//			UpdateWindow(hwnd);
//		}
	} else {
		/* This check is performed here so we can allow files opened from the command line to be used in the existing instance if there is one */
		HANDLE hMutex = CreateMutex(NULL, TRUE, TEXT("AudioPlayer"));
		if (GetLastError() == ERROR_ALREADY_EXISTS) {
//			ErrorMessage(GetLocalText(70));
//			SendMessage(hwnd, WM_CLOSE, 0, 0);
			return 0;
		} else {
			ReleaseMutex(hMutex);
			CloseHandle(hMutex);
		}
		ShowWindow(hwnd, nCmdShow);
		UpdateWindow(hwnd);
	}

	DragAcceptFiles(hwnd, TRUE);

	while (GetMessage(&Msg, NULL, 0, 0)) {
//		if (!TranslateAccelerator(hwnd, hAccel, &Msg)) {
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
//		}
	}

	return (int)Msg.wParam;
}
